<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * library actions.
 *
 * @package    FinalsClub
 * @subpackage library
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class libraryActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	$user = UserPeer::retrieveById($this->getUser()->getId());
  	
  	
  	if($user && $user->getNetworkName() == 'harvard')
  	
  	{
  		$this->upload_access = 'yes';
  	}
  	else
  	{
  		$this->upload_access = 'no';
  	}
  	
  }
  	

  public function executeShow(sfWebRequest $request)
  {
    $this->file = FilePeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->file);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new FileForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new FileForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($file = FilePeer::retrieveByPk($request->getParameter('id')), sprintf('Object file does not exist (%s).', $request->getParameter('id')));
    $this->form = new FileForm($file);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($file = FilePeer::retrieveByPk($request->getParameter('id')), sprintf('Object file does not exist (%s).', $request->getParameter('id')));
    $this->form = new FileForm($file);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($file = FilePeer::retrieveByPk($request->getParameter('id')), sprintf('Object file does not exist (%s).', $request->getParameter('id')));
    $file->delete();

    $this->redirect('library/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $file = $form->save();

      $this->redirect('library/edit?id='.$file->getId());
    }
  }
  
  public function executeProfileList(sfWebRequest $request)
  {		$user_id = $request->getParameter('id');
  		
		return $this->renderComponent('library','profileList',array('user_id'=>$user_id));
  }
  
  public function executeProfileThumbnail(sfWebRequest $request)
  {
  	$user_id = $request->getParameter('id');
  		
  	
  	//if the profile library is of the current user then show all files
		if($user_id == $this->getUser()->getId())
		{
			$this->files = FilePeer::retrieveByOwnerId($user_id);
		}
		else
		{
			$files = FilePeer::retrieveByOwnerId($user_id);
			$result = array();
			foreach($files as $file)
			{
				if(EntitlementPeer::isEntitled($file->getId(), $this->getUser()->getId(), EntitlementPeer::FILE_TYPE, EntitlementPeer::READ_PERMISSION))
				{
				$result[] = $file;
				}
			}
			
			$this->files = $result;
		}
  		
  		
  		return $this->renderPartial('library/profileThumbnail',array('files'=>$this->files));
  }
  
  public function executeProfileCategory(sfWebRequest $request)
  {
  	$user_id = $request->getParameter('id');
  		
  	//make call search based on user id
	$tags = FilePeer::retrieveTagsByOwnerId($user_id);
	
	$size = sizeof($tags);
	$tags_1 = array();
	$tags_2 = array();
	$tags_3 = array();
	$index = 1;
	
	foreach($tags as $tag=>$count)
	{
		if(($index)%3 == 0)
		{
			$tags_3[$tag] = $count;
		}
		elseif(($index)%2 == 0)
		{		
			$tags_2[$tag] = $count;
		}
		else
		{
			$tags_1[$tag] = $count;
		}
		
		$index++;
		if($index > 3)
		{
			$index = 1;
		}
	}
	
	$this->tags = array();
	$this->tags[0] = $tags_1;
	$this->tags[1] = $tags_2;
	$this->tags[2] = $tags_3;
	
	
	return $this->renderPartial('library/profileCategory',array('tags'=>$this->tags));
 
  }
  
  
  public function executePublicDetail(sfWebRequest $request)
  {
  	
  	$page = $request->getParameter('page',1);
  	$filter = $request->getParameter('filter');
  	$sort_by = $request->getParameter('sortby');
  	$title = $request->getParameter('title');
  	
  	//retrieve all summaries and uploaded files that are public
  	$content = LibraryPeer::retrievePublicContent($sort_by,$filter,$title);
  	
  	$per_page = 5;
  	$size = sizeof($content);
  	$page_content = array_slice($content, ($per_page*($page-1)),$per_page); 
  	$pages = ceil($size/$per_page);
  	
  	return $this->renderPartial('library/publicDetail',array('per_page'=>$per_page, 'size'=>$size, 'page_content'=>$page_content, 'pages'=>$pages, 'page'=>$page));
  } 
  
    public function executePublicList(sfWebRequest $request)
  {
  	$page = $request->getParameter('page',1);
  	$filter = $request->getParameter('filter');
  	$sort_by = $request->getParameter('sort_by');
  	$title = $request->getParameter('title');
  	
  	//retrieve all summaries and uploaded files that are public
  	$content = LibraryPeer::retrievePublicContent($sort_by,$filter,$title);
  	
  	$per_page = 5;
  	$size = sizeof($content);
  	$page_content = array_slice($content, ($per_page*($page-1)),$per_page); 
  	$pages = ceil($size/$per_page);
  	
  	return $this->renderPartial('library/publicList',array('per_page'=>$per_page, 'size'=>$size, 'page_content'=>$page_content, 'pages'=>$pages, 'page'=>$page));
  } 
  
  public function executeCourseDetail(sfWebRequest $request)
  {
  	$page = $request->getParameter('page',1);
  	$filter = null;
  	$sort_by = null;
  	$title = null;
  	$course_id = $request->getParameter('course_id');
  	
  	//retrieve all summaries and uploaded files that are public
  	$content_array = LibraryPeer::retrievePublicContentByCourseId($course_id, $sort_by,$filter,$title);
  	
  	$content = array();
  	
  	foreach($content_array as $item)
  	{
  		if(EntitlementPeer::isEntitled($item['obj_id'], $this->getUser()->getId(),$item['type']))
  		{
  			$content[] = $item;
  		}
  		
  	}
  	
  	
  	
  	$per_page = 5;
  	$size = sizeof($content);
  	$page_content = array_slice($content, ($per_page*($page-1)),$per_page); 
  	$pages = ceil($size/$per_page);
  	
  	
  	
  	
  	
  	
  	return $this->renderPartial('library/courseDetail',array('per_page'=>$per_page, 'size'=>$size, 'page_content'=>$page_content, 'pages'=>$pages, 'page'=>$page));
  }
  
  public function executeCourseList(sfWebRequest $request)
  {
  	
  	$page = $request->getParameter('page',1);
  	$filter = null;
  	$sort_by = null;
  	$title = null;
  	$course_id = $request->getParameter('course_id');
  
  	
  	//retrieve all summaries and uploaded files that are public
  	$content_array = LibraryPeer::retrievePublicContentByCourseId($course_id, $sort_by,$filter,$title);
  	
 	$content = array();
  	
  	foreach($content_array as $item)
  	{
  		if(EntitlementPeer::isEntitled($item['obj_id'], $this->getUser()->getId(),$item['type']))
  		{
  			$content[] = $item;
  		}
  		
  	}
  	
  	$per_page = 5;
  	$size = sizeof($content);
  	$page_content = array_slice($content, ($per_page*($page-1)),$per_page); 
  	$pages = ceil($size/$per_page);
  	
  	return $this->renderPartial('library/courseList',array('per_page'=>$per_page, 'size'=>$size, 'page_content'=>$page_content, 'pages'=>$pages, 'page'=>$page));
  }
  
  public function executeGroup(sfWebRequest $request)
  {
  	$user = UserPeer::retrieveById($this->getUser()->getId());
  	$group_id = $request->getParameter('id');
  
  	//group page security	
  	$groupMember = GroupMemberPeer::retrieveByUserIdAndGroupId($this->getUser()->getId(), $request->getParameter('id'));
	$this->group = GroupPeer::retrieveById($request->getParameter('id'));
	
	if($this->getUser()->isAdmin()) {
		
	} else if(!$groupMember) {
		$this->setTemplate('groups/nonmember');
	} else if($groupMember->getStatus() != GroupMemberPeer::APPROVED_STATUS) {
		$this->setTemplate('groups/pending');
	}
  		
  	$this->group = GroupPeer::retrieveById($group_id);
  	
  	
  	//security to show upload button, may not be need with the other group security check
  	if($user && $user->getNetworkName() == 'harvard')
  	{
  		$this->upload_access = 'yes';
  	}
  	else
  	{
  		$this->upload_access = 'no';
  	}
  	
  
  }

public function executeGroupDetail(sfWebRequest $request)
  {
	$page = $request->getParameter('page',1);
  	$filter = $request->getParameter('filter');
  	$sort_by = $request->getParameter('sortby');
  	$title = $request->getParameter('title');
  	$group_id = $request->getParameter('group_id');
  	//retrieve all summaries and uploaded files that are public
  	$content_array = LibraryPeer::retrieveGroupContent($sort_by,$filter,$title,$group_id);
  	
  	$content = array();
  	
  	foreach($content_array as $item)
  	{
  		if(EntitlementPeer::isEntitled($item['obj_id'], $this->getUser()->getId(),$item['type']))
  		{
  			$content[] = $item;
  		}
  		
  	}
  	
  	
  	$per_page = 5;
  	$size = sizeof($content);
  	$page_content = array_slice($content, ($per_page*($page-1)),$per_page); 
  	$pages = ceil($size/$per_page);
  	
  	return $this->renderPartial('library/publicDetail',array('per_page'=>$per_page, 'size'=>$size, 'page_content'=>$page_content, 'pages'=>$pages, 'page'=>$page));
    
  }
  
  public function executeGroupList(sfWebRequest $request)
  {
	$page = $request->getParameter('page',1);
  	$filter = $request->getParameter('filter');
  	$sort_by = $request->getParameter('sortby');
  	$title = $request->getParameter('title');
  	$group_id = $request->getParameter('group_id');
  	
  	//retrieve all summaries and uploaded files that are public
  	$content = LibraryPeer::retrieveGroupContent($sort_by,$filter,$title, $group_id);
  	
  	$per_page = 5;
  	$size = sizeof($content);
  	$page_content = array_slice($content, ($per_page*($page-1)),$per_page); 
  	$pages = ceil($size/$per_page);
  	
  	return $this->renderPartial('library/publicList',array('per_page'=>$per_page, 'size'=>$size, 'page_content'=>$page_content, 'pages'=>$pages, 'page'=>$page));
    
  }
  
}